#!/usr/bin/env ruby

require_relative './questions_graph'

graph = QuestionsGraph.new do
  question 1, '物资补给中遭敌人突袭，你会怎么办？' do
    answer '后退' do
      dp 1
      next_question 2
    end

    answer '防御' do
      dp 1
      next_question 3
    end

    answer '反击' do
      ap 1
      next_question 4
    end
  end

  question 2, '神在哪里？' do
    answer '不存在' do
      klass 'warlock'
      next_question 5
    end

    answer '天上' do
      klass 'bishop'
      next_question 5
    end

    answer '自己心中' do
      next_question 4
    end
  end

  question 3, '无敌部队什么最重要？' do
    answer '破坏力' do
      klass 'gladiator'
      next_question 6
    end

    answer '统帅力' do
      klass 'fighter'
      next_question 6
    end

    answer '机动力' do
      next_question 4
    end
  end

  question 4, '你喜欢下列的什么？' do
    answer '带着海香的海风' do
      klass 'pirate'
      next_question 7
    end

    answer '和煦的春风' do
      klass 'knight'
      next_question 7
    end

    answer '清爽的微风' do
      klass 'hawk knight'
      next_question 7
    end
  end

  question 5, '世界末日到来时，能拯救我们的是什么？' do
    answer '伟大的神' do
      spell 'force heal 1'
      next_question 8
    end

    answer '丰富的知识' do
      spell 'thunder'
      next_question 9
    end

    answer '自己的力量' do
      at 1
      next_question 10
    end
  end

  question 6, '你指挥的部队追求什么？' do
    answer '充足的兵力' do
      mercenary 'lancer'
      next_question 9
    end

    answer '完全的统制' do
      dp 1
      next_question 10
    end

    answer '高昂的士气' do
      ap 1
      next_question 11
    end
  end

  question 7, '理想的部队应该有哪项优秀的能力？' do
    answer '自己的力量比军队更重要' do
      at 1
      mv 1
      next_question 10
    end

    answer '铁壁般的防御力' do
      dp 1
      next_question 11
    end

    answer '粉碎敌人的破坏力' do
      ap 1
      next_question 12
    end
  end

  question 8, '你对黑暗的XX的印象是什么？' do
    answer '邪恶的敌人' do
      spell 'turn undead'
      mp 4
      next_question 13
    end

    answer '崇拜的对象' do
      item 'devil axe'
      next_question 14
    end

    answer '微不足道' do
      item 'mirage robe'
      mp 2
      next_question 15
    end
  end

  question 9, '你为何生存在世上？' do
    answer '保护所爱之人' do
      spell 'protection 1'
      mp 4
      next_question 13
    end

    answer '自我提高' do
      at 2
      df 1
      next_question 14
    end

    answer '实现梦想' do
      spell 'attack 1'
      mp 4
      next_question 15
    end
  end

  question 10, '友军侦察部队被包围，但你自己也很危险，你会对你的部队下什么样的指示？' do
    answer '撤退' do
      item 'speed boots'
      exp 7
      next_question 14
    end

    answer '请求支援，展开防卫战' do
      spell 'protection 1'
      item 'large shield'
      next_question 15
    end

    answer '营救侦察部队' do
      spell 'heal 1'
      item 'cross'
      next_question 16
    end
  end

  question 11, '汝毕生何求？' do
    answer '知识' do
      spell 'thunder'
      mp 4
      next_question 15
    end

    answer '财富' do
      item 'large shield'
      gold 300
      next_question 16
    end

    answer '权力' do
      item 'wand'
      next_question 17
    end
  end

  question 12, '当你拥有强大的力量时，你会为了什么而使用它？' do
    answer '为了守护众生' do
      item 'amulet'
      next_question 15
    end

    answer '为了支配一切' do
      at 1
      df 2
      next_question 16
    end

    answer '为了贯彻正义' do
      mercenary 'monk'
      next_question 17
    end
  end

  question 13, '对你来说爱是什么？' do
    answer '相互给予的东西' do
      spell 'charm'
      mp 1
      next_question 18
    end

    answer '接受的东西' do
      item 'war hammer'
      next_question 18
    end

    answer '无限力量的源泉' do
      at 1
      next_question 18
    end
  end

  question 14, '你想从这个世界获得什么？' do
    answer '爱、希望和勇气' do
      at 1
      next_question 18
    end

    answer '无拘无束的自由' do
      mp 2
      next_question 18
    end

    answer '秩序和统一' do
      df 1
      next_question 18
    end
  end

  question 15, '治国者需要的能力是什么？' do
    answer '个人魅力' do
      spell 'charm'
      mp 1
      next_question 18
    end

    answer '决断力' do
      spell 'quick'
      mp 1
      next_question 18
    end

    answer '实行力' do
      at 1
      next_question 18
    end
  end

  question 16, '要成为英雄必须具备什么？' do
    answer '不屈的精神' do
      mr 10
      next_question 18
    end

    answer '统帅力' do
      dp 1
      next_question 18
    end

    answer '勇气' do
      ap 1
      next_question 18
    end
  end

  question 17, '什么是男人？' do
    answer '包容一切的爱' do
      spell 'heal 1'
      next_question 18
    end

    answer '炽热的灵魂' do
      at 1
      next_question 18
    end

    answer '钢铁般的肉体' do
      dp 1
      next_question 18
    end
  end

  question 18, '战斗开始时，你最想要什么能力？' do
    answer '丰富的战术知识' do
      spell 'heal 1'
      mp 3
      ap 1
      next_question 19
    end

    answer '冷静的判断力' do
      df 1
      mp 2
      dp 1
      next_question 19
    end

    answer '强大的力量' do
      at 1
      df 1
      ap 1
      dp 1
      next_question 19
    end
  end

  question 19, '你理想的部队是什么样的？' do
    answer '少数精锐' do
      ap 1
      dp 1
      capacity(-1)
      next_question 20
    end

    answer '每个人的能力不强，但是是大部队' do
      spell 'attack 1'
      capacity 1
      mp 2
      next_question 20
    end

    answer '自己一人足矣' do
      at 2
      df 1
      next_question 20
    end
  end

  question 20, '最后一问，你为何而战？' do
    answer '为了得到名誉' do
      at 2
    end

    answer '为了保卫祖国' do
      spell 'attack 1'
      mp 1
    end

    answer '为了守护所爱之人' do
      spell 'protection 1'
      df 1
    end
  end
end

def print_max_by(routes, &block)
  route = routes.max_by(&block)
  routes.select{|r| block.(r) == block.(route)}.each.with_index(1) do |r, i|
    puts "********* Candidate #{i} ************"
    puts "AT: #{r.reduce(0){|sum, a| sum + a.at}}"
    puts "DF: #{r.reduce(0){|sum, a| sum + a.df}}"
    puts "A+: #{r.reduce(0){|sum, a| sum + a.ap}}"
    puts "D+: #{r.reduce(0){|sum, a| sum + a.dp}}"
    puts "MP: #{r.reduce(0){|sum, a| sum + a.mp}}"
    puts "MV: #{r.reduce(0){|sum, a| sum + a.mv}}"
    puts "MR: #{r.reduce(0){|sum, a| sum + a.mr}}"
    puts "Class: #{r.reduce(nil){|c, a| a.klass || c}}"
    puts "Capacity: #{r.reduce(0){|sum, a| sum + a.capacity}}"
    puts "Spells: #{r.map{|a| a.spell}.compact.join(', ')}"
    puts "Items: #{r.map{|a| a.item}.compact.join(', ')}"
    puts "Mercenaries: #{r.map{|a| a.mercenary}.compact.join(', ')}"
    puts "Gold: #{r.reduce(0){|sum, a| sum + a.gold}}"
    puts "Exp: #{r.reduce(0){|sum, a| sum + a.exp}}"
    puts "-------------------------------------"
    puts r.map{|a| "#{a.question.text}\n#{a.text}\n\n"}
  end
end

# Top AT, DF, A+, D+, with HEAL 1
print_max_by(graph.routes) {|r| r.reduce(0){|sum, a| sum + a.at + a.df + a.ap + a.dp + (a.spell == 'heal 1' && sum < 1000 ? 1000 : 0)}}

